package com.sskj.mine.invite;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.MineCustomBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/05/03
 */
public class TeamActivity extends BaseActivity<TeamPresenter> {

    @BindView(R2.id.reward_tv)
    TextView rewardTv;
    @BindView(R2.id.reward_total_tv)
    TextView rewardTotalTv;
    @BindView(R2.id.list)
    RecyclerView list;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_team;
    }

    @Override
    public TeamPresenter getPresenter() {
        return new TeamPresenter();
    }

    @Override
    public void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineteamActivity1));
        list.setLayoutManager(new LinearLayoutManager(this));
        list.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setDividerColor(color(R.color.lib_divider)));
//        adapter = SlimAdapter.create().register(R.layout.mine_team_layout, new SlimInjector<MineCustomBean.ListBean>() {
//            @Override
//            public void onInject(MineCustomBean.ListBean inviteList, IViewInjector iViewInjector, List<Object> list) {
//                iViewInjector.text(R.id.id, inviteList.getUsername())
//                        .text(R.id.center, TextUtils.isEmpty(inviteList.getTel()) ? inviteList.getEmail() : inviteList.getTel())
//                        .text(R.id.right, TimeFormatUtil.SF_FORMAT_E.format(Double.valueOf(inviteList.getCreateTime()) * 1000));
//            }
//        }).attachTo(list);

        adapter = SlimAdapter.create().register(R.layout.mine_team_layout, new SlimInjector<MineCustomBean.ListBean>() {
            @Override
            public void onInject(MineCustomBean.ListBean commonBean, IViewInjector iViewInjector, List<Object> list) {
                String time;
                if (commonBean.getCreateTime().contains(" ")) {
                    time = commonBean.getCreateTime().replace(" ", "\n");
                } else {
                    time = commonBean.getCreateTime();
                }
                iViewInjector.text(R.id.id, commonBean.getUsername())
                        .text(R.id.center, commonBean.getLevel().equals("1") ? App.INSTANCE.getString(R.string.mineteamActivity2) : App.INSTANCE.getString(R.string.mineteamActivity3))
                        .text(R.id.right, time);
            }

        }).attachTo(list).updateData(new ArrayList());

        ModelRx2DataSource<MineCustomBean.ListBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<MineCustomBean.ListBean>)
                page -> mPresenter.getRecord(page + "", "20"), 20);
        MVCCoolHelper<List<MineCustomBean.ListBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(adapter);
        mvcHelper.refresh();
    }

    @Override
    public void initData() {

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, TeamActivity.class);
        context.startActivity(intent);
    }

    public void updateUi(MineCustomBean record) {
        adapter.updateData(record.getList());
    }

}
